package 优先算法.二分查找.寻找旋转排序数组中的最小值;

/**
 * https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {1,2};
        int rel = so.findMin(arr);
        System.out.println(rel);
    }
}
class Solution {
    //以左侧为基准
    public int findMin(int[] nums) {
        int left = 0,right = nums.length - 1;
        int rel = nums[left];
        while(left < right){
            int mid = left + (right - left)/2;
            if(nums[mid] < nums[0]){
                right = mid;
            }
            if(nums[mid] >= nums[0]){
                left = mid + 1;
            }
        }
        return rel>nums[left]?nums[left]:rel;
    }
}
class Solution1 {
    //以右侧为基准
    public int findMin(int[] nums) {
        int left = 0,right = nums.length - 1;
         while(left < right){
             int mid = left + (right - left)/2;
             if(nums[mid] < nums[nums.length-1]){
                 right = mid;
             }
             if(nums[mid] > nums[nums.length-1]){
                 left = mid + 1;
             }
         }
         return nums[left];
    }
}